var thead;
var tbody;
var tabIndex;
var tabWidth = '800px';
var tabHeight = '780px' ;
var curClass_head='ui-tab-curr';
var curClass_body="ui-tab-panel ui-tab-curr";
var class_body = "ui-tab-panel";
var tabHeadWidth = 0;//选项卡总共实际宽度
var tabs = [];
var pos_x;//第一个选项卡的位置
var tab_fix =5;//选项卡之间的间隔
var tab_start = 0;//第一个tab的起始位置
var canDelOne = false;//默认第一个也能删除

//记录每个对象的位置信息,长度信息
var tabInfo = new Map();

var tabInfoUrl = new Map();
var iskf = true;//开发时候用

//获取tabHead对象
function getTabHead(id)
{
	thead = document.getElementById(id);
	return thead;
}

function getTabBody(id)
{
	tbody = document.getElementById(id);
	return tbody;
}



var needPP = 0;
function createTab(id,name,u)
{
	var tab_head ;
	var tab_body;
	var tw = 98 ;
	var th = 30 ;
	//先查询是否存在
	tab_head = document.getElementById("page"+id);
	if(tab_head)
	{
		//activeTab(id);
	}
	else
	{
		
		//先获取最后一个的left
		if(tabs.length>0)
		{
			pos_x = tabInfo.get(tabs[tabs.length-1])[0] + tabInfo.get(tabs[tabs.length-1])[1];
			
			//alert(needPP)
			pos_x += tab_fix + needPP;
		}
		else
		{
			pos_x = tab_start;
			
		}
		
		needPP = ((name.length-4)/2)*2;
		tw += (name.length-4)*14;
		
			 
		
		//alert("实际定位"+pos_x + "   "+tw)
		
		
		
		if(tabs.length==0&&!canDelOne)
		{
			tw  = tw - 16;
		}
		//ie6,7下 高度设置成15
		if(b.version==6||b.version==7)
		{
			th = 15;
			tw = tw - 20;
			if(pos_x!=0)
			{
				pos_x += 20+needPP+tab_fix;
			}
		}
		
		tab_head = '<span id="page'+id+'"  style="position: absolute;height:'+th+'px;width:'+tw+'; left:'+pos_x+'px" onclick="activeTab(\''+id+'\')"  >'+name;
		if(tabs.length>0||canDelOne)
		{
			tab_head +='<img style="vertical-align: middle;" src="'+ROOT_PATH+'resource/images/btn-cancel-ready.png" height=16  onclick="removeTab(\''+id+'\')"   />';
		}	
		tab_head += '</span>'
		tab_body = '<div id="tabPage'+id+'"  style="width:'+tabWidth+';height:'+tabHeight+';">'+
		'<iframe  id="ifrm'+id+'" src="'+u+'"  style="margin:0px;padding:0px;border:0px none;overflow-y:hidden;overflow-x:hidden;height:'+tabHeight+';width:100%" frameborder="0" ></iframe></div>';
		
		
		//将对象添加进去
		thead.innerHTML += tab_head;
		//tbody.innerHTML += tab_body;
		
		var tmp = document.createElement("div");
		tmp.id = "tabPage"+id;
		tmp.style.width = tabWidth;
		tmp.style.height = tabHeight;
		tbody.appendChild(tmp);
		tmp.innerHTML = 	'<iframe  id="ifrm'+id+'" src="'+u+'"  style="margin:0px;padding:0px;border:0px none;overflow-y:hidden;overflow-x:hidden;height:'+tabHeight+';width:100%" frameborder="0" ></iframe>';
		tabs[tabs.length] = id;
		
		
		tabInfo.put(id, [pos_x,tw,tab_fix + needPP]);
		tabInfoUrl.put(id,u);
		//判断是否还能放进去tab
		if(tabs.length>5)
		{
			
			//当前宽度tw  当前x  pos_x
			var rest_w = (parseInt(tabWidth)-20 - pos_x);
			
			var a=tabWidth.replace("px", "")-tabHeadWidth
			//alert(rest_w)
			if(rest_w<tw)
			{
			    //调用向左按钮
				startLeft(true);
			}
		}
		//alert(tabWidth+"   "+tabHeadWidth+"   "  +  tabInfo.get(tabs[tabs.length-1])[0] + "   "+tabInfo.get(tabs[tabs.length-1])[1])
		
				
		
		
	}
	activeTab(id);
	
	if(iskf&&typeof(tab_head)=='object')
	{
		eval('ifrm'+id).location.replace(tabInfoUrl.get(id));
	}
}




function activeTab(id)
{
	//alert(tabs.length)
	tabHeadWidth = 0 ;
	for(var i=0;i<tabs.length;i++)
	{
		  //document.getElementById( "page"+ tabs[i]).removeAttribute("class");
		  //alert(document.getElementById( "page"+ tabs[i]).offsetWidth)
		  $('#page'+tabs[i]).removeClass(curClass_head);
		  //alert(parseInt(tabInfo.get(tabs[i])[1])+"     "+parseInt(tabInfo.get(tabs[i])[2]))
		  tabHeadWidth += parseInt(tabInfo.get(tabs[i])[1])+parseInt(tabInfo.get(tabs[i])[2]);
		  //alert(tabInfo.get(tabs[i])[1])
		  var f = document.getElementById( "tabPage"+ tabs[i]);
		  if(f)
		  {
			  document.getElementById( "tabPage"+ tabs[i]).style.display='none';
			//  f.className=class_body;
		  }
		 
		  if(id==tabs[i])
		  {
			  tabIndex = i;
		  }
	}
	
	
	 document.getElementById( "page"+ tabs[tabIndex]).className = curClass_head;
	  //document.getElementById( "tabPage"+ tabs[i]).className=curClass_body;
	  document.getElementById( "tabPage"+ tabs[tabIndex]).style.display='';
	/**
	 * 设置激活的自动显示出来
	 * */
	setActiceShow(tabs[tabIndex]);
	
	setTabsBt();
}


function setActiceShow(id)
{
	//如果当前激活tab是在隐藏的对象里,往右去一个
	if(!isDisplay(id))
	{
		//应该判断隐藏在左边还是右边,并且是隐藏了几个
		if(tabInfo.get(id)[0]<tab_start)
		{
		     trunRight(true);
		}
		else
		{
			startLeft(true);
		}
		
		setActiceShow(id);
	}
}



function removeTab(id)
{
	//只剩一个不能被删除
	if(tabs.length==1)
	{
		return ;
	}
	//判断是否允许删除第一个
	if(!canDelOne)
	{
		if(tabs[0]==id)
		{
			return ;
		}
	}
	
	var w = document.getElementById("page"+id).offsetWidth+tab_fix;
	thead.removeChild(document.getElementById("page"+id));
	tbody.removeChild(document.getElementById("tabPage"+id));
	var newTabs = [] ;
	var reactiveTab = "";
	var removeIndex = 0;
	for(var i=0;i<tabs.length;i++)
	{
		if(tabs[i]!=id)
		{
			newTabs[newTabs.length] = tabs[i];
		}
		else
		{
			//删除的这个就是当前激活的
			removeIndex = i;
			//获取要删除的
			if(i==tabIndex)
			{
				//如果i前面还有,就激活前面一个
				if(i>0)
				{
					reactiveTab = tabs[i-1];
				}
				else
				{
					reactiveTab = tabs[1];
				}
			}
			else
			{
				reactiveTab = tabs[tabIndex];
			}
			
		}
		
	}
	
	//找到前面一个对象
	var ppx = 0;
	var hpx = 0;
	var removeSize = 0;
	//删除的不是最后一个
	if(removeIndex!=(tabs.length-1))
	{
		//拿到后面的位置
		removeSize = tabInfo.get(tabs[removeIndex+1])[0] - tabInfo.get(id)[0];
	}
	tabs=newTabs;
	//将后面的往前移动删除的width
	for(var i=removeIndex;i<tabs.length;i++)
	{
		var npox = tabInfo.get(tabs[i])[0]-removeSize;
		document.getElementById( "page"+ tabs[i]).style.left = npox+"px";
		//判断是不是需要显示
		isDisplay(tabs[i]);
		
		//更新新坐标位置
		tabInfo.put(tabs[i], [npox,tabInfo.get(tabs[i])[1],tabInfo.get(tabs[i])[2]]);
	}
	tabInfo.remove(id);
	//如果不存在激活的就激活当前的前一个
	activeTab(reactiveTab)
}


var startLeftFlag = false;

function startLeft(f)
{
	startLeftFlag = needLeft(f)
	trunLeft();
}
var leftSize=0;//要往左移动的距离
function trunLeft()
{
	
	if(startLeftFlag)
	{
		for(var i=0;i<tabs.length;i++)
		{
			 var tab_head =  document.getElementById( "page"+ tabs[i]);
			 //alert(tab_head.scrollLeft)
			 if(tab_head.style.left)
			 { 
			     
			    //每次移动当前对象的宽度
			    if(parseInt(tab_head.style.left)==tab_start)
		    	{
			    	leftSize = tabInfo.get(tabs[i+1])[0] - tabInfo.get(tabs[i])[0]
			    	//leftSize = leftSize-1;
			    	break;
		    	}
			    //tab_head.style.left = parseInt(tab_head.style.left) - leftSize; 
			    
			 }
		}
		
		for(var i=0;i<tabs.length;i++)
		{
			 var tab_head =  document.getElementById( "page"+ tabs[i]);
			 //alert("要移动的距离"+leftSize)
			 if(tab_head.style.left)
			 { 
			    	
			    	//alert("移动前"+tab_head.style.left);
					tab_head.style.left = (parseInt(tab_head.style.left) - leftSize)+"px"; 
					
					if(isDisplay(tabs[i]))
					{
						tab_head.style.display = '';
					}
					else
					{
						tab_head.style.display = 'none';
					}
					
					tabInfo.put(tabs[i], [parseInt(tab_head.style.left),tabInfo.get(tabs[i])[1],tabInfo.get(tabs[i])[2]]);
					//alert("移动后"+tab_head.style.left)
			    //tab_head.style.left = parseInt(tab_head.style.left) - leftSize; 
			 }
		}
		
		
		//window.setTimeout(trunLeft, 20);
	}
	
	setTabsBt();
}

function stopLeft()
{
	startLeftFlag = false;
}








/**
 * 判断是否需要往左边移动
 */
function needLeft(f)
{
	if(f)
	{
		return f;
	}
	//alert(parseInt(tabHeadWidth)+"    "+ parseInt(tabWidth) )
	//判断最后一个是不是隐藏的
	
	if(document.getElementById("page"+tabs[tabs.length-1]).style.display=="")
	{
		return false;
	}
	//判断最后一个是不是left已经是0了
	var a = document.getElementById("page"+tabs[tabs.length-1]);
	if(a)
	{
		if(parseInt(a.style.left)<=tab_start)
		{
			return false;
		}
	}
	
	
	return true;
}


/**
 * 判断是否需要往右边移动
 */
function needRight(f)
{
	if(f)
	{
		return f;
	}
	//判断第一个是不是left已经是0了
	var a = document.getElementById("page"+tabs[0]);
	if(a)
	{
		if(parseInt(a.style.left)>=tab_start)
		{
			return false;
		}
	}
	
	
	return true;
}



var rightSize=0;//要往右移动的距离
function trunRight(f)
{
	if(needRight(f))
	{
		
		for(var i=tabs.length-1;i>0;i--)
		{
			if(parseInt(document.getElementById( "page"+ tabs[i]).style.left)==tab_start)
			{
				//alert(i + "   "+tabInfo.get(tabs[i-1]))
				rightSize = tab_start - tabInfo.get(tabs[i-1])[0];
				
				//alert("要移动的距离"+rightSize)
				break;
			}
		}
		//alert(rightSize)
		for(var i=tabs.length-1;i>=0;i--)
		{
			 var tab_head =  document.getElementById( "page"+ tabs[i]);
			 //alert(tab_head.scrollLeft)
			 tab_head.style.left = (tabInfo.get(tabs[i])[0] + rightSize)+"px";
			 
			 //alert(tab_head.style.left)
			 isDisplay(tabs[i]);
			 
			 
			 tabInfo.put(tabs[i], [tabInfo.get(tabs[i])[0] + rightSize,tabInfo.get(tabs[i])[1],tabInfo.get(tabs[i])[2]]);
		}
	}
	
	setTabsBt();
}
/**
 * true  表示要显示的
 */
function isDisplay(id)
{
	//判断是不是左边
	var o = document.getElementById("page"+id);
	var l = parseInt(o.style.left);
	if(l<tab_start)
	{
		o.style.display = "none";
		return false;
	}
	
	
	//判断是否要显示原来隐藏的计算左边位置+宽度是否超出整体
	if(l+tabInfo.get(id)[1]>parseInt(tabWidth))
	{
		o.style.display = "none";
		return false;
	}
	
	
	
	o.style.display = "";
	return true;
	
}


//设置滚动按钮
function setTabsBt()
{
	//第一个是显示的不需要向右按钮
	if(isDisplay(tabs[0]))
	{
		document.getElementById("page-2").style.display="none";
	}
	else
	{
		document.getElementById("page-2").style.display="";
	}
	//最后一个是显示的,不需要向左按钮
	if(isDisplay(tabs[tabs.length-1]))
	{
		document.getElementById("page-1").style.display="none";
	}
	else
	{
		document.getElementById("page-1").style.display="";
	}
}



function refresh(fn)
{
	var args = Array.prototype.slice.call(arguments,1);
	eval('ifrm'+tabs[tabIndex]).eval(fn).apply(this,args)
}


